<script setup>
import { listItemCategoryService } from '@/api/item.js'
import { ref } from 'vue'

defineProps({
  modelValue: {
    type: [Number, String]
  },
  width: {
    type: String
  }
})
const emit = defineEmits(['update:modelValue'])
const itemCategoryList = ref([])

// 获取所有物品类别
const getItemCategoryList = async () => {
  const res = await listItemCategoryService()
  // 提取id和name字段
  itemCategoryList.value = res.data.data.map((itemCategory) => ({
    id: itemCategory.id,
    name: itemCategory.name
  }))
}
getItemCategoryList()
</script>

<template>
  <el-select
    :modelValue="modelValue"
    @update:modelValue="emit('update:modelValue', $event)"
    :style="{ width }"
  >
    <el-option
      v-for="itemCategory in itemCategoryList"
      :key="itemCategory.id"
      :label="itemCategory.name"
      :value="itemCategory.id"
    ></el-option>
  </el-select>
</template>
